#include "../include/email_sender.h"

email_sender::email_sender(){
    this->is_connected=true;
    try{
    this->smtp=new Poco::Net::SecureSMTPClientSession(SMTP_SERVER,SMTP_PORT);
    smtp->login(Poco::Net::SMTPClientSession::LoginMethod::AUTH_LOGIN, SENDER, AUTH_CODE);
    }catch(std::exception &e){
        std::cout<<"emial_connection_error: "<<e.what();
        this->is_connected=false;
    }
    
    
}

bool email_sender::send_email_plain_text(const std::string  &recipient,const std::string & subject,const std::string& content)
{
    if(this->is_connected==false){
        std::cout<<"当前未与smtp服务器连接"<<std::endl;
        return false;
    }
    try{
    Poco::Net::MailMessage message;
	message.setSender(SENDER);
    message.addRecipient(Poco::Net::MailRecipient(Poco::Net::MailRecipient::PRIMARY_RECIPIENT, recipient));
    message.setSubject(subject);
	message.setContent(content);
    this->smtp->sendMessage(message);}
    catch(std::exception &e){
        std::cout<<"emial_send_error: "<<e.what();
        return false;
    }
    return true;
}

email_sender::~email_sender()
{
    this->smtp->close();
    delete this->smtp;
}